package com.youyou.fusheng2.View.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.View.Adapter.ViewPagerAdapter;
import com.youyou.fusheng2.View.Fragment.PublicFragment;

import java.util.ArrayList;
import java.util.List;

public class FourButtonActivity extends AppCompatActivity implements View.OnClickListener, TabLayout.OnTabSelectedListener {

    private TextView  home_tetle;
    private TabLayout home_tablayout;
    private ViewPager home_viewpager;
    private ImageView home_back;
    private List<Fragment> fragments   = new ArrayList<>();
    private List<String>   tetleLayoue = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_four_button);
        Bundle text = getIntent().getBundleExtra("text");
        String s = text.getString("s");
        int f = text.getInt("f");
        initView(s);
        initViewpager(f);
    }

    private void initViewpager(int f) {
        tetleLayoue.add("热榜");
        tetleLayoue.add("电影");
        tetleLayoue.add("伦理");
        tetleLayoue.add("动漫");
        for (int i = 0; i < 4; i++) {
            Fragment fragment = new PublicFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("string", i);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        home_tablayout.setupWithViewPager(home_viewpager);
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, tetleLayoue);
        home_viewpager.setAdapter(viewPagerAdapter);
        //点击那个加载那个
        home_viewpager.setCurrentItem(f);
        home_tablayout.getTabAt(f).select();
    }

    private void initView(String s) {
        findViewById(R.id.home_sousuo).setVisibility(View.GONE);
        findViewById(R.id.sousuo_show).setVisibility(View.GONE);
        home_tetle = (TextView) findViewById(R.id.home_tetle);
        home_tablayout = (TabLayout) findViewById(R.id.home_tablayout);
        home_tablayout = (TabLayout) findViewById(R.id.home_tablayout);
        home_viewpager = (ViewPager) findViewById(R.id.home_viewpager);
        home_viewpager.setOffscreenPageLimit(3);
        home_back = (ImageView) findViewById(R.id.home_back);
        home_back.setVisibility(View.VISIBLE);
        home_back.setOnClickListener(this);
        home_tablayout.addOnTabSelectedListener(this);
        //设置数据
        home_tetle.setText(s);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.home_back:
                finish();
                break;
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        home_tetle.setText(tab.getText().toString());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}